//index.tsx
import { useState, useRef, useEffect } from 'react';
// import { message } from 'antd';
import './index.less';

type IntriProps = {
    onChangeIsShow: (childData: any) => void;
};

const rightMenu: React.FC<IntriProps> = ({ onChangeIsShow }) => {
    const menuList: Array<any> = [
        { name: "添加组件", key: "WorldClock", src: "" },
        { name: "更换壁纸", key: "", src: "" },
        { name: "个性化设置", key: "", src: "" },
        { name: "批量删除", key: "", src: "" },
    ]

    const [visible, setVisible] = useState<boolean>(false); //菜单显隐藏
    const contextMenu = useRef<HTMLDivElement | any>(null); //ref
    const currentEvent = useRef<MouseEvent | any>(null); //ref
    const [tops, setTops] = useState<number>();
    const [menuData] = useState<Array<any>>(menuList)

    // 右键事件监听
    const _handleContextMenu = (event: any) => {
        event.preventDefault();
        setVisible(true);
        currentEvent.current = event;

        setTops(event.screenX)
    };
    const _handleClick = (event: any) => {
        const wasOutside = !(event.target.contains === contextMenu);
        // 点击其他位置需要隐藏菜单
        if (wasOutside) setVisible(false);
    };
    const _handleScroll = () => {
        if (visible) setVisible(false);
    };
    useEffect(() => {
        if (!contextMenu.current) return;
        const clickX = currentEvent.current.clientX;
        const clickY = currentEvent.current.clientY;
        const rootW = contextMenu.current.offsetWidth;
        const rootH = contextMenu.current.offsetHeight;
        const screenW = window.innerWidth;
        const screenH = window.innerHeight;
        const right = screenW - clickX > rootW;
        const left = !right;
        const top = screenH - clickY > rootH;
        const bottom = !top;

        if (right) {
            contextMenu.current.style.left = `${clickX + 5}px`;
        }

        if (left) {
            contextMenu.current.style.left = `${clickX - rootW - 5}px`;
        }

        if (top) {
            contextMenu.current.style.top = `${clickY + 5}px`;
        }

        if (bottom) {
            contextMenu.current.style.top = `${clickY - rootH - 5}px`;
        }
    }, [visible, tops]);
    useEffect(() => {
        // 事件监听
        document.addEventListener('contextmenu', _handleContextMenu);
        document.addEventListener('click', _handleClick);
        document.addEventListener('scroll', _handleScroll);
        return () => {
            // 组件卸载移除事件监听
            document.removeEventListener('contextmenu', _handleContextMenu);
            document.removeEventListener('click', _handleClick);
            document.removeEventListener('scroll', _handleScroll);
        };
    }, []);
    // 菜单事件处理
    const menuHandle = (data: any) => {
        setVisible(false);

        if (data.key === 'WorldClock') {
            onChangeIsShow(true)
        }
    };
    return (
        visible && (
            <div ref={contextMenu} className='contextMenu'>
                {
                    menuData.map(item => (
                        <div key={item.key} className='menuList' onClick={() => menuHandle(item)}>
                            <img src={item.src} alt="" />
                            <span>{item.name}</span>
                        </div>
                    ))
                }
            </div>
        )
    );
};

export default rightMenu;

